home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 February / PCpro_2005_02.ISO / files / opensource / jEdit_4.2 / jedit42install.exe / {app} / jedit.jar / bsh / BSHArrayInitializer.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-08-29  |  3.3 KB  |  80 lines

  1. package bsh;
  2.  
  3. import java.lang.reflect.Array;
  4.  
  5. class BSHArrayInitializer extends SimpleNode {
  6.    BSHArrayInitializer(int id) {
  7.       super(id);
  8.    }
  9.  
  10.    public Object eval(CallStack callstack, Interpreter interpreter) throws EvalError {
  11.       throw new EvalError("Array initializer has no base type.", this, callstack);
  12.    }
  13.  
  14.    public Object eval(Class baseType, int dimensions, CallStack callstack, Interpreter interpreter) throws EvalError {
  15.       int numInitializers = this.jjtGetNumChildren();
  16.       int[] dima = new int[dimensions];
  17.       dima[0] = numInitializers;
  18.       Object initializers = Array.newInstance(baseType, dima);
  19.  
  20.       for(int i = 0; i < numInitializers; ++i) {
  21.          SimpleNode node = (SimpleNode)this.jjtGetChild(i);
  22.          Object currentInitializer;
  23.          if (node instanceof BSHArrayInitializer) {
  24.             if (dimensions < 2) {
  25.                throw new EvalError("Invalid Location for Intializer, position: " + i, this, callstack);
  26.             }
  27.  
  28.             currentInitializer = ((BSHArrayInitializer)node).eval(baseType, dimensions - 1, callstack, interpreter);
  29.          } else {
  30.             currentInitializer = node.eval(callstack, interpreter);
  31.          }
  32.  
  33.          if (currentInitializer == Primitive.VOID) {
  34.             throw new EvalError("Void in array initializer, position" + i, this, callstack);
  35.          }
  36.  
  37.          Object value;
  38.          if (currentInitializer instanceof Primitive) {
  39.             Primitive primValue = (Primitive)currentInitializer;
  40.             if (baseType.isPrimitive()) {
  41.                try {
  42.                   primValue = primValue.castToType(baseType, 0);
  43.                } catch (UtilEvalError e) {
  44.                   e.printStackTrace();
  45.                   Interpreter.debug("error:" + e);
  46.                   this.throwTypeError(baseType, primValue, i, callstack);
  47.                }
  48.             }
  49.  
  50.             value = primValue.getValue();
  51.          } else {
  52.             value = currentInitializer;
  53.          }
  54.  
  55.          try {
  56.             Array.set(initializers, i, value);
  57.          } catch (IllegalArgumentException e) {
  58.             Interpreter.debug("illegal arg" + e);
  59.             this.throwTypeError(baseType, currentInitializer, i, callstack);
  60.          } catch (ArrayStoreException e) {
  61.             Interpreter.debug("arraystore" + e);
  62.             this.throwTypeError(baseType, currentInitializer, i, callstack);
  63.          }
  64.       }
  65.  
  66.       return initializers;
  67.    }
  68.  
  69.    private void throwTypeError(Class baseType, Object initializer, int argNum, CallStack callstack) throws EvalError {
  70.       String rhsType;
  71.       if (initializer instanceof Primitive) {
  72.          rhsType = ((Primitive)initializer).getType().getName();
  73.       } else {
  74.          rhsType = Reflect.normalizeClassName(initializer.getClass());
  75.       }
  76.  
  77.       throw new EvalError("Incompatible type: " + rhsType + " in initializer of array type: " + baseType + " at position: " + argNum, this, callstack);
  78.    }
  79. }
  80.